package com.yoomiito.app.ui.songduoduo.freegoods

import android.text.TextUtils
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.sdd.SddFreeGoodsData
import com.yoomiito.app.model.sdd.ShareMsg
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.utils.YMToast


class FreeGoodsListPresenter(appConfig: AppConfig) :BaseActPresenter<FreeGoodsListActivity>(appConfig){

    fun list(goodsId:String,shareId:String,page:Int){
        YmApi.getYmService()
                .sddFreeGoodsAllList(goodsId,shareId,page,20)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<SddFreeGoodsData>(){
                    override fun onSuccess(t: SddFreeGoodsData?) {
                        v?.data(t)
                    }

                    override fun onFail(error: ApiException?) {
                        error?.msg?.showToast()
                    }

                })
    }

    /**
     * 送多多--获取分享信息
     * @param type
     * @param goodsId
     */
    fun getSddShareMsg(type: Int) {
        YmApi.getYmService()
                .shareMsg(type, "")
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<ShareMsg?>() {
                    override fun onSuccess(shareMsg: ShareMsg?) {
                        v.sddShareMsg(shareMsg!!)
                    }

                    override fun onFail(error: ApiException) {
                        v.dismissLoading()
                        if (!TextUtils.isEmpty(error.msg)) YMToast.showCenterToast(error.msg)
                    }
                })
    }
}